﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;

namespace Dwarf.Managed
{
    /// <summary>
    /// User interface for solving the Unstructured Grid Problem.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The settings for this dwarf.
        /// </summary>
        static private Settings settings;

        /// <summary>
        /// Init & fill neigbourhood array
        /// </summary>
        static private void constructNeighborhood(Solver ugsolver)
        {
            //Init map
            var neighbors = new HashSet<int>[ugsolver.numVertex];
            for (int j = 0; j < ugsolver.numVertex; j++)
            {
                neighbors[j] = new HashSet<int>();
            }

            //Insert verteces into map
            for (int j = 0; j < ugsolver.numTriangles; j++)
            {
                neighbors[ugsolver.triangles[0][j]].Add(ugsolver.triangles[1][j]);
                neighbors[ugsolver.triangles[0][j]].Add(ugsolver.triangles[2][j]);

                neighbors[ugsolver.triangles[1][j]].Add(ugsolver.triangles[0][j]);
                neighbors[ugsolver.triangles[1][j]].Add(ugsolver.triangles[2][j]);

                neighbors[ugsolver.triangles[2][j]].Add(ugsolver.triangles[0][j]);
                neighbors[ugsolver.triangles[2][j]].Add(ugsolver.triangles[1][j]);
            }

            //Copy from map to array
            for (int i = 0; i < ugsolver.numVertex; i ++)
            {
                ugsolver.neighborhood[i] = neighbors[i].ToArray<int>();
            }
        }

        /// <summary>
        /// Get settings from the command line.
        /// </summary>
        /// <param name="args">
        /// Cli params.
        /// </param>
        /// <param name="ugsolver">
        /// Instance of Solver.
        /// </param>
        static private void getSettings(String[] args, Solver ugsolver)
        {
            settings = new Settings();
            settings.Init(args, ugsolver);

            constructNeighborhood(ugsolver);
        }

        /// <summary>
        /// Point of the program start.
        /// </summary>
        /// <param name="args">
        /// Cli params.
        /// </param>
        static void Main(String[] args)
        {
            try
            {
                Solver ugsolver = new Solver();

                getSettings(args, ugsolver);    // Get settings from the command line.

                settings.Start();               // Start new time count.
                ugsolver.Solve();               // Solve the current problem.
                settings.Finish(ugsolver);      // Stop the time count and write results.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                usage();
            }

        }

        /// <summary>
        /// Write the rules of command line structure.
        /// </summary>
        [DllImport(@"CliTools.dll")]
        private static extern void usage();
    }
}
